﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Win.Templates;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using aztall.Module.BusinessObjects;
using DevExpress.XtraEditors.Controls;

namespace aztall.Module.Win.Controllers
{
  // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
  public partial class DomainSelectorController : ViewController<DetailView>
  {
    const string NullText = "Please select a value";

    LookUpEdit domainsControl;

    public DomainSelectorController()
    {
      InitializeComponent();
      // Target required Views (via the TargetXXX properties) and create their Actions.
      this.TargetViewId = "DomainSelector";
    }
    protected override void OnActivated()
    {
      base.OnActivated();
      // Perform various tasks depending on the target View.
      this.Frame.GetController<DevExpress.ExpressApp.SystemModule.DialogController>().CancelAction.Execute += CancelAction_Execute;
      this.Frame.GetController<DevExpress.ExpressApp.SystemModule.DialogController>().AcceptAction.Execute += AcceptAction_Execute;
    }

    private void AcceptAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
      Env.CurrentDomainCode = domainsControl.Text;
      Env.CurrentDomainOid = (int)domainsControl.EditValue;
    }

    private void CancelAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
      base.Application.Exit();
    }

    protected override void OnDeactivated()
    {
      // Unsubscribe from previously subscribed events and release other references and resources.
      base.OnDeactivated();
    }
    protected override void OnViewControlsCreated()
    {
      base.OnViewControlsCreated();
      domainsControl = this.View.FindItem("Domains").Control as LookUpEdit;
      if (domainsControl != null)
      {
        Guid[] currentUser = new Guid[] { ((User)SecuritySystem.CurrentUser).Oid };
        var domains = ObjectSpace.GetObjects<Domain>(new ContainsOperator("Users", new InOperator("Oid", currentUser))).OrderBy(c => c.DomainCode).ToList();
        domainsControl.Properties.DataSource = domains;
        //domainsControl.Properties.ForceInitialize();
        //domainsControl.Properties.PopulateColumns();
        domainsControl.Properties.DisplayMember = "DomainCode";
        domainsControl.Properties.ValueMember = "Oid";
        domainsControl.Properties.NullValuePrompt = NullText;
        domainsControl.Properties.NullValuePromptShowForEmptyValue = true;
        domainsControl.Properties.Columns.Add(new LookUpColumnInfo("DomainCode", "Domain Code"));
        domainsControl.Properties.SortColumnIndex = 0;
        domainsControl.EditValue = domains[0].Oid;
      }
    }
    protected override void OnFrameAssigned()
    {
      base.OnFrameAssigned();
      Frame.TemplateViewChanged += Frame_TemplateViewChanged;
    }

    private void Frame_TemplateViewChanged(object sender, EventArgs e)
    {
      //if (Frame.View.Id == "DomainSelector")
      //{
      IWindowTemplate t = Frame.Template as IWindowTemplate;
      Control templateControl = (Control)Frame.Template;
      templateControl.HandleCreated += (s, args) =>
      {
        PopupForm f = templateControl as PopupForm;
        if (f != null)
        {
          f.MinimizeBox = false;
          f.MaximizeBox = false;
          f.AutoShrink = true;
          f.InitialMinimumSize = new System.Drawing.Size(100, 100);
                //f.FormBorderStyle = FormBorderStyle.FixedDialog;
                //f.Size = new System.Drawing.Size(80, 20);
              }
      };
      //}
    }
  }
}
